<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="终端序号" prop="terminalId">
        <el-input
          v-model="queryParams.terminalId"
          placeholder="请输入终端序号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="终端名称" prop="terminalName">
        <el-input
          v-model="queryParams.terminalName"
          placeholder="请输入终端名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="终端类型" prop="terminalGroup">
        <el-input
          v-model="queryParams.terminalGroup"
          placeholder="请输入终端类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电量" prop="kwh">
        <el-input
          v-model="queryParams.kwh"
          placeholder="请输入电量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="信号" prop="xinhao">
        <el-input
          v-model="queryParams.xinhao"
          placeholder="请输入信号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据" prop="shuju">
        <el-input
          v-model="queryParams.shuju"
          placeholder="请输入数据"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供电方式" prop="modeId">
        <el-input
          v-model="queryParams.modeId"
          placeholder="0:电池供电,1:供电异常"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="消警" prop="xiaojing">
        <el-input
          v-model="queryParams.xiaojing"
          placeholder="0:关闭,1:打开"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="0:离线,1:在线"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改用户" prop="updateUser">
        <el-input
          v-model="queryParams.updateUser"
          placeholder="请输入修改用户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="添加用户" prop="createUser">
        <el-input
          v-model="queryParams.createUser"
          placeholder="请输入添加用户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="删除状态" prop="deleteState">
        <el-input
          v-model="queryParams.deleteState"
          placeholder="0在线,-1离线"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mk:wireless:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mk:wireless:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mk:wireless:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['mk:wireless:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wirelessList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="终端序号" align="center" prop="terminalId" />
      <el-table-column label="终端名称" align="center" prop="terminalName" />
      <el-table-column label="终端类型" align="center" prop="terminalGroup" />
      <el-table-column label="电量" align="center" prop="kwh" />
      <el-table-column label="信号" align="center" prop="xinhao" />
      <el-table-column label="数据" align="center" prop="shuju" />
      <el-table-column label="供电方式" align="center" prop="modeId" />
      <el-table-column label="消警" align="center" prop="xiaojing" />
      <el-table-column label="状态" align="center" prop="state" />
      <el-table-column label="修改用户" align="center" prop="updateUser" />
      <el-table-column label="添加用户" align="center" prop="createUser" />
      <!-- <el-table-column label="删除状态" align="center" prop="deleteState" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mk:wireless:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mk:wireless:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改终端管理对话框 -->
    <el-dialog :title="title" :visible.sync="open"  width="500px"  append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="终端序号" prop="terminalId">
          <el-input v-model="form.terminalId" placeholder="请输入终端序号" />
        </el-form-item>
        <el-form-item label="终端名称" prop="terminalName">
          <el-input v-model="form.terminalName" placeholder="请输入终端名称" />
        </el-form-item>
        <el-form-item label="终端类型" prop="terminalGroup">
          <el-input v-model="form.terminalGroup" placeholder="请输入终端类型" />
        </el-form-item>
        <el-form-item label="电量" prop="kwh">
          <el-input v-model="form.kwh" placeholder="请输入电量" />
        </el-form-item>
        <el-form-item label="信号" prop="xinhao">
          <el-input v-model="form.xinhao" placeholder="请输入信号" />
        </el-form-item>
        <el-form-item label="数据" prop="shuju">
          <el-input v-model="form.shuju" placeholder="请输入数据" />
        </el-form-item>
        <el-form-item label="供电方式" prop="modeId">
          <el-input v-model="form.modeId" placeholder="请输入供电方式(0:电池供电,1:供电异常)" />
        </el-form-item>
        <el-form-item label="消警" prop="xiaojing">
          <el-input v-model="form.xiaojing" placeholder="请输入消警(0:关闭,1:打开)" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态(0:离线,1:在线)" />
        </el-form-item>
        <!-- <el-form-item label="修改用户" prop="updateUser">
          <el-input v-model="form.updateUser" placeholder="请输入修改用户" />
        </el-form-item> -->
        <el-form-item label="添加用户" prop="createUser">
          <el-input v-model="form.createUser" placeholder="请输入添加用户" />
        </el-form-item>
        <!-- <el-form-item label="删除状态" prop="deleteState">
          <el-input v-model="form.deleteState" placeholder="请输入删除状态" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWireless, getWireless, delWireless, addWireless, updateWireless, exportWireless } from "@/api/mk/wireless";

export default {
  name: "Wireless",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 终端管理表格数据
      wirelessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        terminalId: null,
        terminalName: null,
        terminalGroup: null,
        kwh: null,
        xinhao: null,
        shuju: null,
        modeId: null,
        xiaojing: null,
        state: null,
        updateUser: null,
        createUser: null,
        deleteState: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        terminalId: [
          { required: true, message: "终端序号不能为空", trigger: "blur" }
        ],
        terminalName: [
          { required: true, message: "终端名称不能为空", trigger: "blur" }
        ],
        terminalGroup: [
          { required: true, message: "终端类型不能为空", trigger: "blur" }
        ],
        kwh: [
          { required: true, message: "电量不能为空", trigger: "blur" }
        ],
        xinhao: [
          { required: true, message: "信号不能为空", trigger: "blur" }
        ],
        shuju: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        modeId: [
          { required: true, message: "供电方式(0:电池供电,1:供电异常)不能为空", trigger: "blur" }
        ],
        xiaojing: [
          { required: true, message: "消警(0:关闭,1:打开)不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "状态(0:离线,1:在线)不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
        updateUser: [
          { required: true, message: "修改用户不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "添加时间不能为空", trigger: "blur" }
        ],
        createUser: [
          { required: true, message: "添加用户不能为空", trigger: "blur" }
        ],
        // deleteState: [
        //   { required: true, message: "删除状态(0在线,-1离线)不能为空", trigger: "blur" }
        // ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询终端管理列表 */
    getList() {
      this.loading = true;
      listWireless(this.queryParams).then(response => {
        this.wirelessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        terminalId: null,
        terminalName: null,
        terminalGroup: null,
        kwh: null,
        xinhao: null,
        shuju: null,
        modeId: null,
        xiaojing: null,
        state: null,
        updateTime: null,
        updateUser: null,
        createTime: null,
        createUser: null,
        deleteState: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加终端管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWireless(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改终端管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWireless(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWireless(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除终端管理编号为"' + ids + '"的数据项？').then(function() {
        return delWireless(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有终端管理数据项？').then(() => {
        this.exportLoading = true;
        return exportWireless(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
